package com.bootdo.staff.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.staff.domain.StaffUserDO;
import com.bootdo.staff.service.StaffUserService;
import com.bootdo.common.domain.DictDO;
import com.bootdo.common.service.DictService;
import com.bootdo.common.utils.MD5Utils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;


/**
 * 员工表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-04-23 13:59:00
 */
@Controller
@RequestMapping("/staff/staffUser")
public class StaffUserController {
	
	@Autowired
	private StaffUserService staffUserService;
	
	@Autowired
	private DictService dictService;
	
	@GetMapping()
//	@RequiresPermissions("staff:staffUser:staffUser")
	String StaffUser(){
	    return "staff/staffUser/staffUser";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("staff:staffUser:staffUser")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<StaffUserDO> staffUserList = staffUserService.list(query);
		int total = staffUserService.count(query);
		PageUtils pageUtils = new PageUtils(staffUserList, total);
		return pageUtils;
	}
	
//	获取状态集合
	@ResponseBody
	@GetMapping("/status")
	public List<StaffUserDO> listStatus() {
		
		return staffUserService.listStatus();
	}
	
	
	@GetMapping("/add")
	String add(){
	    return "staff/staffUser/add";
	}

	@GetMapping("/edit/{staffId}")
	//@RequiresPermissions("staff:staffUser:edit")
	String edit(@PathVariable("staffId") Long staffId,Model model){
		StaffUserDO staffUser = staffUserService.get(staffId);
		model.addAttribute("staffUser", staffUser);
	    return "staff/staffUser/edit";
	}
	
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( StaffUserDO staffUser){
//		staffUser.setSmenuIds((List<Long>) (session.getAttribute("smenuIds")));
		//密码用md5加密	
		staffUser.setStaffPassword(MD5Utils.encrypt(staffUser.getName(), staffUser.getStaffPassword()));
		staffUser.setRegistTime(new Date());//注册时间默认当前时间
//		staffUser.setLoginTime(new Date());//暂时设定当前时间
		if(staffUserService.save(staffUser)>0){
			return R.ok();
		}
		return R.error(1, "保存失败");		
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( StaffUserDO staffUser){
		staffUserService.update(staffUser);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long staffId){
		if(staffUserService.remove(staffId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] staffIds){
		staffUserService.batchRemove(staffIds);
		return R.ok();
	}
}